<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>借阅管理</title>
    <link href="../../component/pear/css/pear.css" rel="stylesheet"/>
</head>
<body class="pear-container">
<!--检索组件-->
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">书名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="bookname" id="select_bookname" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">出版社</label>
                    <div class="layui-input-inline">
                        <input type="text" name="press" id="select_press" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">作者</label>
                    <div class="layui-input-inline">
                        <input type="text" name="author" id="select_author" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="role-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<!--信息显示组件-->
<div class="layui-card">
    <div class="layui-card-body">
        <table id="book-table" lay-filter="book-table"></table>
    </div>
</div>
<!--催还组件-->
<div style="display: none" id="showBorrow">
    <form class="layui-form" action="" lay-filter="bookDetail">
        <div class="mainBox">
            <div class="main-container">
                <table border="1" cellpadding="0">
                    <thead align="center">
                    <td>书名</td>
                    <td>借书时间</td>
                    <td>最后还书时间</td>
                    <td>作者</td>
                    <td>类别</td>
                    </thead>
                    <tbody align="center">
                    <input type="hidden" width="80" id="bookid" name="id">
                    <input type="hidden" width="80" id="userid" name="userid">
                    <td width="80" id="bookname"></td>
                    <td width="180" id="lendTime"></td>
                    <td width="180" id="sendTime"></td>
                    <td width="80" id="author"></td>
                    <td width="100" id="categoryId"></td>
                    </tbody>
                </table>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="button" class="layui-btn layui-btn-sm layui-btn-danger" lay-submit=" "
                        lay-filter="send-submit">
                    催还
                </button>
                <button id="close" type="button" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="">
                    <i class="layui-icon layui-icon-close"></i>
                    关闭
                </button>
            </div>
        </div>
    </form>
</div>
<!--操作按钮组件-->
<script type="text/html" id="book-bar">
    <button class="pear-btn pear-btn-warming pear-btn-sm" lay-event="power"><i
            class="layui-icon layui-icon-vercode"></i></button>
</script>
<script src="../../component/layui/layui.js"></script>
<script src="../../component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;

        let MODULE_PATH = "operate/";
        //设置表头信息
        let cols = [
            [
                {
                    title: '用户id',
                    field: 'userid',
                    align: 'center',
                    hide: true
                },
                {
                    title: '书名',
                    field: 'bookname',
                    align: 'center',
                    templet: '<div>{{d.book.bookname}}</div>'
                },
                {
                    title: 'ISBN',
                    field: 'book.isbn',
                    align: 'center',
                    templet: '<div>{{d.book.isbn}}</div>'
                },
                {
                    title: '出版社',
                    field: 'book.press',
                    align: 'center',
                    templet: '<div>{{d.book.press}}</div>'
                },
                {
                    title: '类别',
                    field: 'book.categoryId',
                    align: 'center',
                    templet: leibie
                },
                {
                    title: '作者',
                    field: 'book.author',
                    align: 'center',
                    templet: '<div>{{d.book.author}}</div>'
                },
                {
                    title: '剩余数量',
                    field: 'book.surplus',
                    align: 'center',
                    templet: '<div>{{d.book.surplus}}</div>'
                },
                {
                    title: '总数',
                    field: 'book.number',
                    align: 'center',
                    sort: true,
                    templet: '<div>{{d.book.number}}</div>'
                },
                {
                    title: '借书时间',
                    field: 'lendTime',
                    align: 'center',
                    templet: "<div>{{layui.util.toDateString(d.lendTime,'yyyy-MM-dd')}}</div>"
                },
                {
                    title: '借书人',
                    field: 'lend',
                    align: 'center',
                    templet: '<div>{{d.user.account}}</div>'
                },

                {
                    title: '操作',
                    toolbar: '#book-bar',
                    align: 'center'
                }
            ]
        ]

        function leibie(d) {
            let str;
            if (d.book.categoryId == 1) {
                str = '计算机类';
            } else if (d.book.categoryId == 2) {
                str = '文史类';
            } else if (d.book.categoryId == 3) {
                str = '社科类';
            }
            return str;
        }

        //表格数据填充
        table.render({
            elem: '#book-table',
            url: 'http://47.93.226.229/getBorrowBook',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#role-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('tool(book-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'power') {
                window.power(obj);
            }
        });

        table.on('toolbar(book-table)', function (obj) {
            console.log(obj);
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(role-query)', function (data) {
            data.field.page = 1;
            data.field.limit = 5;
            $.ajax({
                url: 'http://47.93.226.229/getBorrowBook',
                data: data.field,//data，规定要发送服务器的数据
                type: "GET",//规定请求的类型
                dataType: "json",//预期服务器相应的类型
                success: function (a) {
                    table.reload('book-table', {
                        where: {
                            bookname: $("#select_bookname").val(),
                            press: $("#select_press").val(),
                            author: $("#select_author").val(),
                        }, page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    })
                }
            });
            return false;
        });

        window.power = function (obj) {
            let data = obj.data; //获得当前行数据
            let index = 0;
            $.get(
                'http://47.93.226.229/selectBook' + data.book.bookid, function (obj1) {
                    index = layer.open({
                        type: 1,
                        title: '详情',
                        shade: 0.1,
                        area: ['600px', '320px'],
                        content: $('#showBorrow')
                    })

                    // form.val("bookDetail",data)
                    content: $("#book_id").val(data.id);
                    content: $("#userid").val(data.userid);
                    // content: $("#bookId").text(obj1.data.bookId);
                    content: $("#bookname").text(data.book.bookname);
                    let created = layui.util.toDateString(data.lendTime, "yyyy-MM-dd");
                    content: $("#lendTime").text(created);
                    let created1 = layui.util.toDateString(data.sendTime, "yyyy-MM-dd");
                    content: $("#sendTime").text(created1);
                    if (data.book.categoryId == 1) {
                        data.book.categoryId = '计算机类';
                    } else if (data.book.categoryId == 2) {
                        data.book.categoryId = '文史类';
                    } else if (data.book.categoryId == 3) {
                        data.book.categoryId = '社科类';
                    }
                    content: $("#categoryId").text(data.book.categoryId);
                    content: $("#author").text(data.book.author);
                })


            form.on('submit(send-submit)', function (data) {

                $.ajax({
                    url: "http://47.93.226.229/sendEmail",
                    data: {"id": data.field.userid},
                    type: "POST",
                    success: function (d) {
                        if (d.code > 0) {
                            layer.msg(d.msg);
                            layer.close(index);
                        }
                    }
                });
            });
        };

        window.refresh = function () {
            table.reload('book-table');
        }
    })
</script>
</body>
</html>
